package wnhz.com.business.groupshop.adapter;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseRVAdapter;
import wnhz.com.business.base.BaseRecyclerAdapter;
import wnhz.com.business.base.BaseViewHolder;
import wnhz.com.business.base.RecyclerViewHolder;
import wnhz.com.business.groupshop.bean.OrderBean;
import wnhz.com.business.uitls.decoration.HorizDecoration;
import wnhz.com.business.impl.ITaskFinishListener;
import wnhz.com.business.uitls.LogUtils;
import wnhz.com.business.view.MyListView;

/**
 * Created by Administrator on 2017/6/29 0029.
 */

public class MianTwoAdapter extends BaseRecyclerAdapter<OrderBean.InfoBean> {
    static final String TAG = "MianTwoAdapter";

    private MyItemClickLinstener ClickLinstener;
    private ProgressDialog mProgressDialog;
    private OrderBean orderBean;

    public MianTwoAdapter(Context context, List<OrderBean.InfoBean> datas) {
        super(context, datas, R.layout.item_main_two_layout);
    }

    public void setOnClickListener(MyItemClickLinstener listener) {
        ClickLinstener = listener;
    }

    @Override
    protected void loadData(int page, final ITaskFinishListener iTaskFinishListener) {
//        if (mProgressDialog == null) {
//            mProgressDialog = new ProgressDialog(context);
//            mProgressDialog.setCancelable(true);
//            mProgressDialog.setCanceledOnTouchOutside(false);
//        }
//        mProgressDialog.setMessage("加载中...");
//        mProgressDialog.show();
//
//        SharedPreferences sp = context.getSharedPreferences("user", Context.MODE_PRIVATE);
//        String token = sp.getString("token", "");
//        Map<String, Object> map = new HashMap<>();
//        map.put("token", token);
//
//        XUtil.Post(Url.ORDER_MYORDER, map, new MyCallBack<String>() {
//            @Override
//            public void onSuccess(String result) {
//                super.onSuccess(result);
//                LogUtils.e("----------------", "---------------" + result);
//                try {
//                    JSONObject jsonObject = new JSONObject(result);
//                    String re = jsonObject.optString("re");
//                    String url = jsonObject.optString("url");
//                    if (re.equals("1")) {
//                        Gson gson = new Gson();
//                        orderBean = gson.fromJson(result, OrderBean.class);
//                        infoList = orderBean.getInfo();
//                        iTaskFinishListener.onSuccess(TAG, infoList);
//                    } else if (re.equals("0")) {
//                        iTaskFinishListener.onSuccess(TAG, infoList);
//                    }
//
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                    if (mProgressDialog != null) {
//                        mProgressDialog.dismiss();
//                    }
//                }
//            }
//
//            @Override
//            public void onFinished() {
//                super.onFinished();
//                if (mProgressDialog != null) {
//                    mProgressDialog.dismiss();
//                }
//            }
//
//            @Override
//            public void onError(Throwable ex, boolean isOnCallback) {
//                super.onError(ex, isOnCallback);
//                if (mProgressDialog != null) {
//                    mProgressDialog.dismiss();
//                }
//            }
//        });

        List<OrderBean.InfoBean> infoList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            OrderBean.InfoBean infoBean = new OrderBean.InfoBean();
            infoList.add(infoBean);
        }

        iTaskFinishListener.onSuccess(TAG, infoList);
    }

    @Override
    protected void bindViewHolder(int position, RecyclerViewHolder holder) {
        TextView tv_remind = holder.getView(R.id.tv_remind, false);
        ImageView img_pq_status = holder.getView(R.id.img_pq_status, false);
        TextView tv_time = holder.getView(R.id.tv_time, false);

        MyListView my_list_view = holder.getView(R.id.my_list_view_order, false);
        RecyclerView recycler = holder.getView(R.id.recycler, false);


//        final OrderBean.InfoBean item = getItem(position);
//        final List<OrderBean.InfoBean.GoodsBean> listOrder = item.getGoods();

        List<OrderBean.InfoBean.GoodsBean> goodsBeanList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            OrderBean.InfoBean.GoodsBean goodsBean = new OrderBean.InfoBean.GoodsBean();
            goodsBeanList.add(goodsBean);
        }

//        my_list_view.setAdapter(new FragmentOneItemAdapter(context, goodsBeanList));
        recycler.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recycler.addItemDecoration(new HorizDecoration(1));
        recycler.setAdapter(new BaseRVAdapter(context, goodsBeanList) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_main_two_item_layout;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                ImageView img = holder.getView(R.id.iv_img);
                Glide.with(context).load("http://pic2.ooopic.com/13/60/34/41b1OOOPICa4.jpg").into(img);

                //中间画横线
                holder.getTextView(R.id.tv_oldprice).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            }
        });

        tv_remind.setTag(R.id.tv_remind, position);
        tv_remind.setOnClickListener(itemClickLinstener);
    }

    @Override
    protected void setPageSize(int pageSize) {
    }

    private View.OnClickListener itemClickLinstener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (ClickLinstener != null) {
                int position;
                switch (v.getId()) {
                    case R.id.tv_remind:
                        position = (int) v.getTag(R.id.tv_remind);
                        LogUtils.e("==============" + TAG, "---------------------------------" + position);
                        ClickLinstener.OnDrawbackClickListener(MianTwoAdapter.this, position, v);
                        break;
                }
            }
        }
    };


    public interface MyItemClickLinstener {

        void OnDrawbackClickListener(MianTwoAdapter adapter, int position, View view);
    }
}
